package com.ohuang.demo.kotlin

data class TrueData<T>(val boolean: Boolean, val data: T)
data class FalseData<T>(val boolean: Boolean, val data: T)

infix fun <T> Boolean.ifTrue(v1: T): TrueData<T> {
    return TrueData(this, v1)
}

infix fun <T> Boolean.ifFalse(v1: T): FalseData<T> {
    return FalseData(this, v1)
}

infix fun <T> TrueData<T>.elseTo(v1: T): T {
    return if (boolean) data else v1
}

infix fun <T> FalseData<T>.elseTo(v1: T): T {
    return if (boolean) v1 else data
}

class Case() {
    fun breakCase() {
        throw BreakException()
    }
}

class BreakException : Exception()
open class Switch() {
    private var isDefault = true
    fun case(boolean: Boolean, block: Case.() -> Unit) {
        if (boolean) {
            if (isDefault) isDefault = false
            Case().block()
        }
    }

    fun default(block: () -> Unit) {
        if (isDefault) {
            block()
        }
    }
}

class ValueSwitch<T>(val value: T) : Switch() {

    fun case(t: T, block: Case.() -> Unit) {
        case(value == t, block)
    }

}

fun switch(block: Switch.() -> Unit) {
    val switch = Switch()
    try {
        switch.block()
    } catch (e: BreakException) {
        // do nothing
    }
}

fun <T> switch(value: T, block: ValueSwitch<T>.() -> Unit) {
    val switch = ValueSwitch(value)
    try {
        switch.block()
    } catch (e: BreakException) {
        // do nothing
    }
}

